﻿Imports System.Data.Odbc
Imports System.Data.SqlClient

Public Class frmConceptos
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Dim CadenasAutoCompletado As New AutoCompleteStringCollection
    Dim Concepto As New DataTable("Conceptos")

    Private Sub frmConceptos_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró a la asignacion de Rutas > Conceptos")
        Usuario.Ubicacion("Ventana Principal")
        Concepto.PrimaryKey = Nothing
        Concepto.Clear()
        Concepto.Columns.Clear()
    End Sub

    Private Sub frmConceptos_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgConceptos.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó a la asignacion de Rutas > Conceptos")
        Usuario.Ubicacion("Asignación Rutas > Conceptos")

        Try
            CargaConceptos()
            Dim Claves(0) As DataColumn
            Claves(0) = Concepto.Columns("Nombres")
            Concepto.PrimaryKey = Claves

            For Each Nombre In Concepto.Rows
                CadenasAutoCompletado.Add(Nombre("Nombres"))
            Next
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        End Try
        CargaRutas()
    End Sub
    Sub CargaRutas()
        Dim cnConn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select cCodigoV01,Nombre FROM Rutas"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql
            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader
            Dim TipoDiesel As String = ""
            Dim CodigoRuta As String = ""
            Dim NombreRuta As String = ""
            Dim NombreConceptoDiesel As String = ""
            Dim TipoPagobus As String = ""
            Dim NombreConceptoPagobus As String = ""
            Dim TipoVentas As String = ""
            Dim NombreConceptoVentas As String = ""

            Do While rdBuscar.Read()
                CodigoRuta = rdBuscar("cCodigoV01").ToString.Trim
                NombreRuta = rdBuscar("Nombre").ToString.Trim

                NombreConceptoDiesel = BuscarStrDatoCitra("Select NombreConcepto FROM Conceptos Where CodigoRuta='" & CodigoRuta & "' AND Tipo='Diesel'")
                TipoDiesel = BuscarStrDatoCitra("Select Tipo FROM Conceptos Where CodigoRuta='" & (rdBuscar("cCodigoV01").ToString.Trim & "' and NombreConcepto='" & NombreConceptoDiesel & "'"))

                NombreConceptoPagobus = BuscarStrDatoCitra("Select NombreConcepto FROM Conceptos Where CodigoRuta='" & CodigoRuta & "' AND Tipo='Pagobus'")
                TipoPagobus = BuscarStrDatoCitra("Select Tipo FROM Conceptos Where CodigoRuta='" & (rdBuscar("cCodigoV01").ToString.Trim & "' and NombreConcepto='" & NombreConceptoPagobus & "'"))

                NombreConceptoVentas = BuscarStrDatoCitra("Select NombreConcepto FROM Conceptos Where CodigoRuta='" & CodigoRuta & "' AND Tipo='Ventas'")
                TipoVentas = BuscarStrDatoCitra("Select Tipo FROM Conceptos Where CodigoRuta='" & (rdBuscar("cCodigoV01").ToString.Trim & "' and NombreConcepto='" & NombreConceptoVentas & "'"))


                dgConceptos.Rows.Add(CodigoRuta, NombreRuta, NombreConceptoDiesel, TipoDiesel)
                dgConceptos.Rows.Add(CodigoRuta, NombreRuta, NombreConceptoPagobus, TipoPagobus)
                dgConceptos.Rows.Add(CodigoRuta, NombreRuta, NombreConceptoVentas, TipoVentas)
               
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Sub CargaConceptos()
        Concepto.Columns.Add("Nombres")

        Dim cnConn As New SqlConnection
        Dim strSql As String = ""
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * FROM MGW10006"
            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql
            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader

            Do While rdBuscar.Read()
                Concepto.Rows.Add(rdBuscar("cNombreC01").ToString.Trim)
            Loop
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Aviso")
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try
    End Sub
    Private Sub ControlEdicion(ByVal Sender As Object, ByVal e As DataGridViewEditingControlShowingEventArgs) Handles dgConceptos.EditingControlShowing
        If dgConceptos.CurrentCell.ColumnIndex = 2 Then
            DirectCast(e.Control, TextBox).AutoCompleteMode = AutoCompleteMode.SuggestAppend
            DirectCast(e.Control, TextBox).AutoCompleteSource = AutoCompleteSource.CustomSource
            DirectCast(e.Control, TextBox).AutoCompleteCustomSource = CadenasAutoCompletado
        ElseIf dgConceptos.CurrentCell.ColumnIndex = 3 Then
        Else

            DirectCast(e.Control, TextBox).AutoCompleteMode = Nothing
        End If
    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub ToolStripButton4_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton4.Click
        Dim Con As New clsConceptos
        Dim i As Integer = 0
        Dim IdConcepto As Integer = 0

        For i = 0 To dgConceptos.Rows.Count - 1
            IdConcepto = BuscarIntDatoCitra("Select IdConcepto FROM Conceptos Where CodigoRuta='" & dgConceptos.Rows(i).Cells(0).Value & "' and NombreConcepto='" & dgConceptos.Rows(i).Cells(2).Value & "'")
            If dgConceptos.Rows(i).Cells(2).Value <> "" Then
                If IdConcepto > 0 Then
                    If dgConceptos.Rows(i).Cells(3).Value = "" Then
                        MsgBox("Debes indicar el tipo de concepto", MsgBoxStyle.Exclamation, "Aviso")
                        Exit Sub
                    End If
                    Con.IdConcepto = IdConcepto
                    Con.CodigoConcepto = BuscarStrDatoCitra("MGW10006", "cCodigoC01", "cNombreC01='" & dgConceptos.Rows(i).Cells(2).Value & "'")
                    Con.NombreConcepto = dgConceptos.Rows(i).Cells(2).Value
                    Con.CodigoRuta = dgConceptos.Rows(i).Cells(0).Value
                    Con.NombreRuta = dgConceptos.Rows(i).Cells(1).Value
                    Con.Tipo = dgConceptos.Rows(i).Cells(3).Value
                    Con.Actualizar()
                    Bitacora.RegistraActividad("Actualizó el concepto de la ruta " & dgConceptos.Rows(i).Cells(1).Value & " del tipo: " & dgConceptos.Rows(i).Cells(3).Value)
                Else
                    If dgConceptos.Rows(i).Cells(3).Value = "" Then
                        MsgBox("Debes indicar el tipo de concepto", MsgBoxStyle.Exclamation, "Aviso")
                        Exit Sub
                    End If
                    Con.IdConcepto = (BuscarIntDatoCitra("Select max(IdConcepto) as IdConcepto FROM Conceptos")) + 1
                    Con.CodigoConcepto = BuscarStrDatoCitra("MGW10006", "cCodigoC01", "cNombreC01='" & dgConceptos.Rows(i).Cells(2).Value & "'")
                    Con.NombreConcepto = dgConceptos.Rows(i).Cells(2).Value
                    Con.CodigoRuta = dgConceptos.Rows(i).Cells(0).Value
                    Con.NombreRuta = dgConceptos.Rows(i).Cells(1).Value
                    Con.Tipo = dgConceptos.Rows(i).Cells(3).Value
                    Con.Insertar()
                    Bitacora.RegistraActividad("Guardó el concepto de la ruta " & dgConceptos.Rows(i).Cells(1).Value & " del tipo: " & dgConceptos.Rows(i).Cells(3).Value)
                End If
            End If
        Next

        Close()
    End Sub
End Class